package com.example.mvpdemo.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author:created by njb
 * 时间:2019/3/8 15 19
 * 描述:
 */
public abstract class BaseFragment <P extends BasePresenter> extends Fragment implements BaseView {

    public Context context;

    // 控件是否初始化完成
    public boolean isViewCreated;

    // 当前fragment是否加载过数据,如加载过数据，则不再加载
    protected boolean isLoadCompleted;
    //是不是可见
    public boolean isUIVisible;

    protected Unbinder unbinder;

    protected P presenter;
    private View rootView;

    private ProgressDialog dialog;
    private View view;
    private ImageView iv_loading;


    // 懒加载,强制子类重写
    protected abstract void loadData();

    protected abstract int getLayoutId();

    protected abstract void initView();

    protected abstract void addListener();

    protected abstract P createPresenter();


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isUIVisible = isVisibleToUser;
        if (isVisibleToUser && isViewCreated && isUIVisible && !isLoadCompleted) {
            isLoadCompleted = true;
            loadData();
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (isViewCreated && isUIVisible) {
            loadData();
            isLoadCompleted = true;
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(getLayoutId(), container, false);
        unbinder = ButterKnife.bind(this, rootView);
        presenter = createPresenter();

        initView();
        addListener();
        isViewCreated = true;
        return rootView;
    }

    public View getRootView() {
        return rootView;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        isUIVisible = !hidden;
        isLoadCompleted = !hidden;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        if (presenter != null) {
            presenter.detachView();
        }
        if (unbinder != null) {
            unbinder.unbind();
        }
    }

    /**
     * 打开指定的activity
     *
     * @param cls
     */
    public void startA(@NonNull Class<?> cls) {
        Intent intent = new Intent(context, cls);
        startActivity(intent);
    }



    /**
     * toast
     *
     * @param msg
     */
    public void showtoast(@NonNull String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }

    /**
     * 显示加载动画
     */
    public void showLoadingDialog() {
      /*  if (dialog != null && dialog.isShowing()) {
            return;
        }
        if (view == null) {
            view = getViewByRes(R.layout.dialog_loading);
        }
        if (iv_loading == null) {
            iv_loading = view.findViewById(R.id.iv_loading);
        }
        RequestOptions options = new RequestOptions()
                .centerInside()
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);

        Glide.with(context).load(R.mipmap.loading).apply(options).into(iv_loading);
        if (dialog == null) {
            dialog = new ProgressDialog(context, R.style.dialog);
            dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND | WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }
        dialog.setCancelable(false);
        dialog.show();
        dialog.setContentView(view);*/
    }

    /**
     * 隐藏加载动画
     */
    public void closeLoadingDialog() {
        if (dialog != null && dialog.isShowing()) {
            view.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (dialog != null && dialog.isShowing()) {
                        dialog.dismiss();
                    }
                }
            }, 300);
        }
    }


    @Override
    public void onStop() {
        super.onStop();
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    /**
     * 通过资源res获得view
     *
     * @param res
     * @return
     */
    public View getViewByRes(@LayoutRes int res) {
        return LayoutInflater.from(context).inflate(res, null);
    }


    /**
     * 获得TextView 的文本
     *
     * @param tv
     * @return
     */
    public String getTV(TextView tv) {
        return tv == null ? "" : tv.getText().toString().trim();
    }


    private void showFileDialog() {
    }

    private void hideFileDialog() {
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void hideLoading() {
        closeLoadingDialog();
    }

    @Override
    public void showError(String msg) {
        showtoast(msg);
    }

    @Override
    public void onErrorCode(Model model) {
        if (model != null) {
        }
    }

    @Override
    public void showLoadingFileDialog() {
        showFileDialog();
    }

    @Override
    public void hideLoadingFileDialog() {
        hideFileDialog();
    }


    @Override
    public void onProgress(long totalSize, long downSize) {
    }


}
